<%@ page language="java" errorPage="/error.jsp" pageEncoding="UTF-8"
    contentType="text/html;charset=utf-8"%>
<!DOCTYPE html> 
<html>
<head>
<title>代码表详细信息</title>
<jsp:include page="/common/head.jsp"></jsp:include>
<style>
.summer-input{
	width: 170px;
	margin-left:0;
}
</style>    
</head>
<body  class="iframe-page">  
	 <div class="page-title-wrapper" style="height:100px">
	   <div class="page-title">
	     <div class="page-title-text">
            <span>代码表管理</span>
            <span class="split"></span>
            <span>代码表详情</span><br/><br/>
         </div>
         <a id="goBack" class="btn-return distance-left" href="javascript:;"><i class="icon icon-arrow-left"></i>返回主页</a>
	   </div>
	   <h3>${codeVo.name} --- ${codeVo.type}</h3>
	   <div class="page-title-border"></div>
	 </div>

	<div id="t-wrap">
	    <table class="summer-datagrid" id="dataList">
		    <col width="25%">
		    <col width="25%">
		    <col width="25%">
		    <col width="25%">
		    <thead>
			    <tr>
			        <th>名称</th>
					<th>代码</th>
					<th>排序号</th>
			        <th>操作</th>
			    </tr>
		    </thead>
	    </table>
	</div>
     
     <form id="addForm" method="post">
        <div class="search-field">
		    <span class="field-cont">
		        <span class="field-cont-title"><span class="field-required">*</span>名称</span>
		        <input type="text" id="name" name="name" class="summer-input summer-validatebox" data-validatebox="tipPosition:'bottom',validType:['required','maxLength[50]']" />
                <input type="hidden" id="parentId" name="parentId" value="${codeVo.id }"/>
                <input type="hidden" id="type" name="type" value="${codeVo.type}"/>
		    </span>
    
		    <span class="field-cont">
		        <span class="field-cont-title"><span class="field-required">*</span>代码</span>
		        <input type="text" id="code" name="code" class="summer-input"/>
		    </span>
		    
		    <span class="field-cont">
		        <span class="field-cont-title"><span class="field-required">*</span>排序号</span>
		        <input type="text" id="orderNo" name="orderNo" class="summer-input summer-validatebox" data-validatebox="tipPosition:'bottom',validType:['required','betweenInteger[1,100000]']" />
		    </span>
    
		    <span class="field-btn-wrapper" id="funcbar">
               <a class="btn btn-inline" id="closeBtn">返回</a>
		    </span>
		</div>
    </form>
<script>
var CODE_DETAIL_URL = {
        LIST: basePath + '/sys/codeDetail/list',
        ADD: basePath + "/sys/codeDetail/add",
        INIT_EDIT: basePath + '/sys/codeDetail/initEdit',
        DELETE: basePath + '/sys/codeDetail/delete',
        CHECKEXIST: basePath + "/sys/codeDetail/checkCodeDetailExist",
        INIT_CODE_MAIN: basePath + '/sys/code/init'
};
var  permission = CommonUtil.getPermission('code');

$(function() {
	//初始化funcbar
	$('#funcbar').prepend($.funcbar({
        type: 'button',
        data: permission,
        permit: ['codeDetail:add']
    }));
	
	//初始化dataTable
	createTable();
	
	//表单提交事件
    $('#funcbar').on('click', 'a[data-permit="codeDetail:add"]', save);
	//返回事件
    $('#closeBtn,#goBack').on('click', backHome);
	//校验代码code是否可用  
	validateExist('#code',['code'], CODE_DETAIL_URL.CHECKEXIST, ['required','maxLength[50]','regexp'], "代码可用");
	
	//编辑
	$('#t-wrap').on('click', 'a[data-permit="codeDetail:initEdit"]', function() {
		var $this = $(this),
		    id = $this.closest('tr').find('td[data-field="id"]').attr('data-value');
		
		window.location.href = CODE_DETAIL_URL.INIT_EDIT+ "?id=" + id +"&sysId=${param.id}";
	});
	
	//删除
	$('#t-wrap').on('click', 'a[data-permit="codeDetail:delete"]', function() {
		var $this = $(this),
		    id = $this.closest('tr').find('td[data-field="id"]').attr('data-value');
		$.sdialog({
			type: 'confirm',
			msg: '您确定要删除选中的记录吗?',
			onConfirm: function(result) {
			    if (result) {
			    	$.sajax({
		                url : CODE_DETAIL_URL.DELETE + "?ids=" + id +"&type=${codeVo.type}",
		                onLoadSuccess : function() {
		                	 $.stip({
            	                type:'ok',//显示类型 有info、 ok、 danger
            	                text: '删除成功！'    
		                	});
		                	reloadTable();  
		                	parent.refreshPage("${codeVo.id}");
		                }
		            });
			    		
			    }	
			}
		});
	});
});


//重新加载表格
function reloadTable() {
	$('#dataList').datagrid('reload', {
		ajax : {
			param : {
				"parentId": '${codeVo.id}'
			}
		}
	});
}

//初始化列表
function createTable() {
	$('#dataList').datagrid({
		ajax : {
			url : CODE_DETAIL_URL.LIST,
			param : {
				"parentId": '${codeVo.id}'
			}
		},
		pagination: {
			enable: true,
			pageSize: 5,             // 每页显示条目数
            showPageList: false,     // 是否显示自定义pageSize
            showPageInfo: true,     // 是否显示分页信息
		},
		columns : [ 
		  {
			field : 'id',
			hidden : true
		  }, 
		  {
			field : 'name'
		  }, 
		  {
			field : 'code'

		  },
		  {
			field : 'orderNo'
		  }, 
		  {
			render : function(rowData) {
				return $.funcbar({
					data : permission, //请求的数据
					type : 'text', //类型
					permit : ['codeDetail:initEdit', 'codeDetail:delete'] //需要的权限按钮
				});
			}
		  }
		]
	});
}

//清空表单数据
function cleanAddForm(){
     $('#addForm #name').val("");
     $('#addForm #code').val("");
     $('#addForm #orderNo').val("");
}
//保存代码表详情
function save(){
    $("#addForm").form({
        onSubmitSuccess: function (data) {
        	//重新加载表格数据
        	reloadTable();
        	//左侧树局部刷新
            parent.refreshPage("${codeVo.id}");
        	//form表单清空
            cleanAddForm();
        },
        ajax: {
            url: CODE_DETAIL_URL.ADD
        }
    }).form("submit");
}

//返回级联代码页面
function backHome() {
	parent.location.href = CODE_DETAIL_URL.INIT_CODE_MAIN;
}
//校验代码code是否可用  
function validateExist(id, name, url, validType, message) {
    $(id).validatebox({
        rules: {
            regexp: {
                validator: function (value,param) {
                    var result = false,
                        params = {}, 
                        key = name;
                        params[key] = value;
                        params['type'] = '${codeVo.type}';
                        //params['parentId'] = '${param.id}';
                    $.ajax({
                        type: 'post',
                        url: url,
                        data: params,
                        dataType: 'json',
                        async: false,
                        success: function (data) {
                            if (data.result == 'success') {
                                result = true;
                            } else {
                                result = false;
                            }
                        }
                    });
                    return result;
                },
                message: message,
            }
        },
        validType: validType,
        tipPosition: 'bottom'
    });
}
</script>
</body>
</html>
